import self
from selenium import webdriver
import unittest
import time
import os
from selenium.common.exceptions import NoAlertPresentException
from selenium.common.exceptions import NoSuchElementException


class Baidu1(unittest.TestCase):

    # def setUp(self):
    #  print("-----setUp-----")
    #  self.driver = webdriver.Chrome()
    #  self.url = "https://www.baidu.com/"
    #  self.driver.maximize_window()
    #  time.sleep(3)
    def setUp(self):
        print("-----setup------")
        self.driver = webdriver.Chrome()
        self.url = "https:/www.baidu.com"
        self.driver.maximize_window()
        time.sleep(3)

    # def tearDown(self):
    #     print("-----tearDown-----")
    #     self.driver.quit()
    def tearDown(self) -> None:
        print("------tearDown----")
        self.driver.quit()

    # def test_hao(self):
    #     driver = self.driver
    #     url = self.url
    #     driver.get(url)
    #     driver.find_element_by_link_text("hao123").click()
    #     time.sleep(6)
    # @unittest.skip("skipping")
    def test_hao(self):
        driver = self.driver
        url = self.url
        driver.get(url)
        driver.find_element_by_link_text("hao123").click()
        time.sleep(3)

    # def test_baidu(self):
    #     driver = self.driverTIME
    #     url = self.url
    #     driver.get(url)
    #     driver.find_element_by_id("kw").send_keys("python")
    #     driver.find_element_by_id("su").click()
    #     time.sleep(5)
    #     print(driver.title)
    #     try:
    #         self.assertEqual("java_百度搜索", driver.title, msg="实际结果与预期结果不符")
    #     except:
    #         self.saveScreenAsPhoto(driver, "title.png")
    #     time.sleep(3)
    def test_baidu(self):
        driver = self.driver
        url = self.url
        driver.get(url)
        driver.find_element_by_id("kw").send_keys("python")
        driver.find_element_by_id("su").click()
        time.sleep(3)
        try:
            self.assertEqual("java_百度搜索", driver.title, msg="实际结果与预期结果不符")
        except:
            self.savaScreenASPhoto(driver, "title.png")
            time.sleep(3)

    # def saveScreenAsPhoto(self, driver, file_name):
    #     if not os.path.exists("./image"):
    #         os.makedirs("./image")
    #     now = time.strftime("%Y%m%d-%H%M%S", time.localtime(time.time()))
    #     driver.get_screenshot_as_file("./image/" + now + "-" + file_name)
    #     time.sleep(3)
    def savaScreenASPhoto(self, driver, file_name):
        if not os.path.exists("./image"):
            os.makedirs("./image")
        now = time.strftime("%Y&M&D-%H&M%S", time.localtime(time.time()))
        driver.get_screenshot_as_file("./image" + now + "file_name")
        time.sleep(3)


# if __name__ == "__main__":
#     unittest.main()
if __name__ == "__main__":
    unittest.main()

